package com.battery.web.controller.system.battery;

import java.util.List;

import com.battery.common.utils.DateUtils;
import com.battery.system.domain.BO.WithdrawalBO;
import com.battery.system.domain.VO.WithdrawalVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.battery.common.annotation.Log;
import com.battery.common.enums.BusinessType;
import com.battery.system.domain.Withdrawal;
import com.battery.system.service.IWithdrawalService;
import com.battery.common.core.controller.BaseController;
import com.battery.common.core.domain.AjaxResult;
import com.battery.common.utils.poi.ExcelUtil;
import com.battery.common.core.page.TableDataInfo;

/**
 * 提现记录Controller
 *
 * @author battery
 * @date 2021-04-16
 */
@Controller
@RequestMapping("/system/withdrawal")
public class WithdrawalController extends BaseController {
    private String prefix = "system/withdrawal";

    @Autowired
    private IWithdrawalService withdrawalService;

    @RequiresPermissions("system:withdrawal:view")
    @GetMapping()
    public String withdrawal() {
        return prefix + "/withdrawal";
    }

    /**
     * 查询提现记录列表
     */
    @RequiresPermissions("system:withdrawal:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WithdrawalBO withdrawalBO) {
        startPage();
//        List<Withdrawal> list = withdrawalService.selectWithdrawalList(withdrawal);
        List<WithdrawalVO> list = withdrawalService.selectWithdrawalVOList(withdrawalBO);
        return getDataTable(list);
    }

    /**
     * 导出提现记录列表
     */
    @RequiresPermissions("system:withdrawal:export")
    @Log(title = "提现记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Withdrawal withdrawal) {
        List<Withdrawal> list = withdrawalService.selectWithdrawalList(withdrawal);
        ExcelUtil<Withdrawal> util = new ExcelUtil<Withdrawal>(Withdrawal.class);
        return util.exportExcel(list, "withdrawal");
    }

    /**
     * 新增提现记录
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存提现记录
     */
    @RequiresPermissions("system:withdrawal:add")
    @Log(title = "提现记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Withdrawal withdrawal) {
        return toAjax(withdrawalService.insertWithdrawal(withdrawal));
    }

    /**
     * 修改提现记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        Withdrawal withdrawal = withdrawalService.selectWithdrawalById(id);
        mmap.put("withdrawal", withdrawal);
        return prefix + "/edit";
    }


    /**
     * 提现拒绝审核
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/refusePass/{id}")
    public String refusePass(@PathVariable("id") String id, ModelMap mmap) {
        Withdrawal withdrawal = withdrawalService.selectWithdrawalById(id);
        mmap.put("withdrawal", withdrawal);
        return prefix + "/refusePass";
    }


    /**
     * 提现申请通过
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/adoptTx/{id}")
    public String adoptTx(@PathVariable("id") String id, ModelMap mmap) {
        Withdrawal withdrawal = withdrawalService.selectWithdrawalById(id);
        mmap.put("withdrawal", withdrawal);
        mmap.put("type", withdrawal.getType());
        return prefix + "/adoptTx";
    }


    /**
     * 提现申请拒绝
     *
     * @param withdrawal
     * @return
     */
    @RequiresPermissions("system:withdrawal:refusePass")
    @Log(title = "审核拒绝", businessType = BusinessType.UPDATE)
    @PostMapping("/refusePass")
    @ResponseBody
    public AjaxResult refusePass(Withdrawal withdrawal) {
        if (StringUtils.isBlank(withdrawal.getId())) {
            return error("主键ID为空，请联系管理员");
        }
        if (StringUtils.isBlank(withdrawal.getRemark())) {
            return error("请填写拒绝理由");
        }
        withdrawal.setRefuseTime(DateUtils.getNowDate());
        int i = withdrawalService.updateWithdrawal(withdrawal);
        Withdrawal withdrawalOld = withdrawalService.selectWithdrawalById(withdrawal.getId());
        if (withdrawalOld.getIsReturn() == 0) {
            withdrawalService.txErr(withdrawalOld.getId(), withdrawalOld.getAccountType());
        }
        return toAjax(i);
    }


    /**
     * 提现审核
     *
     * @param withdrawal
     * @return
     */
    @RequiresPermissions("system:withdrawal:adoptTx")
    @Log(title = "提现申请-通过", businessType = BusinessType.OTHER)
    @PostMapping("/adoptTx")
    @ResponseBody
    public AjaxResult adoptTx(Withdrawal withdrawal) {
        if (StringUtils.isBlank(withdrawal.getId())) {
            return error("主键ID为空");
        }
        if (withdrawal.getType() == null) {
            return error("请选择提现方式");
        }
        return withdrawalService.adoptTx(withdrawal.getId(), withdrawal.getType());
    }

    /**
     * 修改保存提现记录
     */
    @RequiresPermissions("system:withdrawal:edit")
    @Log(title = "提现记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Withdrawal withdrawal) {
        return toAjax(withdrawalService.updateWithdrawal(withdrawal));
    }

    /**
     * 删除提现记录
     */
    @RequiresPermissions("system:withdrawal:remove")
    @Log(title = "提现记录", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(withdrawalService.deleteWithdrawalByIds(ids));
    }
}
